package de.hbrs.ooka.entities;

import de.hbrs.ooka.entities.dtos.UserTransferObject;
import de.hbrs.ooka.enums.Role;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "userentity")
@NamedQueries({
    @NamedQuery(name = "findUserByID", query = "SELECT u FROM UserEntity u WHERE u.id = :id"),
    @NamedQuery(name = "findUserByName", query = "SELECT u FROM UserEntity u WHERE u.m_name = :name"),
    @NamedQuery(name = "findAllUsers", query = "SELECT u FROM UserEntity u")
})
public class UserEntity implements UserTransferObject, Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String m_name = "admin";
    private String m_password = "admin";
    private int m_roles = 1;
    
    @OneToMany(fetch= FetchType.EAGER)
    private List<ConferenceEntity> m_conferences = new ArrayList<ConferenceEntity>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return m_name;
    }

    @Override
    public List<Role> getRoles() {
        List<Role> roles = new ArrayList<Role>();

        if ((this.m_roles & Role.PARTICIPANT.getDBCode()) == Role.PARTICIPANT.getDBCode()) {
            roles.add(Role.PARTICIPANT);
        }
        if ((this.m_roles & Role.ORGANIZER.getDBCode()) == Role.ORGANIZER.getDBCode()) {
            roles.add(Role.ORGANIZER);
        }
        if ((this.m_roles & Role.REVIEWER.getDBCode()) == Role.REVIEWER.getDBCode()) {
            roles.add(Role.REVIEWER);
        }

        return roles;
    }

    public void addRole(Role r) {
        this.m_roles |= r.getDBCode();
    }

    public void setName(String m_name) {
        this.m_name = m_name;
    }

    @Override
    public String getPassword() {
        return m_password;
    }

    public void setPassword(String m_password) {
        this.m_password = m_password;
    }

    @Override
    public List<ConferenceEntity> getConferences() {
        return m_conferences;
    }

    public void setConferences(List<ConferenceEntity> conferences) {
        this.m_conferences = conferences;
    }

    public void addConference(ConferenceEntity conference) {
        System.out.println("UserEntity, addConference: " + conference.getName());
        if(!this.m_conferences.contains(conference)){
            this.m_conferences.add(conference);
        }
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) id;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserEntity)) {
            return false;
        }
        UserEntity other = (UserEntity) object;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "UserEntity[ id=" + id + " ] Props: " + this.m_name + " - " + Arrays.toString(this.getRoles().toArray());
    }

    public void setRoles(String[] roles) {
        this.m_roles = 1;
        for (String role : roles) {      
            this.addRole(Role.valueOf(Role.class, role.toUpperCase()));
        }
    }
}
